<!DOCTYPE html>
<p class="ui-widget ui-widget-content" style="padding-left: 1em;">	
	<span>班级：</span>
	<select id="bjSel" onchange="bjselChange()">
		<option value="" style="display: none;"></option>
		<option th:each="bj : ${classes}" th:value="${bj.id}" th:text="${bj.name}">1班</option>
	</select>
	<span style="margin-left: 1em;">学生：</span>
	<select id="stuSel" onchange="stuSelChange()">
		<option value="" style="display: none;"></option>
		
	</select>
	<button id="btn" type="button" onclick="selectByBjIdAndkcId()" disabled="disabled">查询</button>
</p>
<table id="list2"></table>
<div id="pager2"></div>

<script type="text/javascript">
var lastrow;
var lastcell;
var options={
		url : 'grade/stuList',
		caption : '按学生录入成绩',
		//width: '100%',
		editurl: 'grade/edit',
		autowidth: true,
		cellEdit: true,
		cellurl:'grade/celledit',
	    beforeEditCell: function (rowid, cellname, v, iRow, iCol) {
	          lastrow = iRow;  //给全局变量赋值
	          lastcell = iCol;
	    },
		height : 500,
		hidegrid: false,
		datatype : 'json',
		mtype : 'post',
		rowNum : 10,
		postData: {stuId:0},
		rowList : [10,20,50],
		prmNames: {search:"search"},
		sortname : 'id',
		viewrecords : true,
		pager : '#pager2',
		jsonReader : { 
			 repeatitems: false,
			 id : 'id'
		},
		colModel:[
			{label:'id',name:'id',index:'id',align:'center',sortable:true,width:50,sorttype:'int',search:false,hidden:true}, 
			{label:'课程',name:'course.name',index:'course.name',align:'center',sortable:true,width:100,sorttype:'text',editable:false}, 
			{label:'成绩',name:'grade',index:'grade',align:'center',sortable:true,width:80,editable:true,editrules:{integer:true},sorttype:'int'}
		]
	};
	$(function(){
		$('#list2').jqGrid(options);
	}); 
	
	function bjselChange(){
		var bjId=$('#bjSel').val();
		$('#btn').attr('disabled',true);
		var stuSel=$('#stuSel');
		stuSel.empty();
		stuSel.append("<option value='' style='display:none;'></option>");
		$.post('student/findStuByClassId',{classId:bjId},function(data){
			for(var i=0;i<data.length;i++){
				var stu=data[i];
				var option=$('<option/>').val(stu.id).text(stu.name);
				stuSel.append(option);
			}
		});
		savecell();
	};
	
	function stuSelChange(){
		$('#btn').attr('disabled',false);
		savecell();
	};
	
	function selectByBjIdAndkcId(){
		var stuId=$('#stuSel').val();
		var postdata={stuId:stuId};
		options.postData=postdata;
		$("#list2").jqGrid('clearGridData');
		$("#list2").setGridParam(options).trigger("reloadGrid");
	};
	
	function savecell(){
		if(lastrow&&lastcell){
			$('#list2').jqGrid("saveCell", lastrow, lastcell);  //当前单元格退出编辑模式
			lastrow=null;
			lastcell=null;
		};
		$("#list2").jqGrid('clearGridData');
	}; 
</script>
